import requests
from requests.auth import HTTPBasicAuth

# RabbitMQ管理界面的地址
url = "http://10.200.1.61:15672/api/exchanges"
# 认证信息：用户名和密码
auth = HTTPBasicAuth("admin", "admin")

# 获取所有交换器
response = requests.get(url, auth=auth)
exchanges = response.json()

# 删除交换器
for exchange in exchanges:
    exchange_name = exchange["name"]
    # 忽略默认的交换器，因为它们不能被删除
    if exchange_name not in (
        "",
        "amq.direct",
        "amq.fanout",
        "amq.headers",
        "amq.match",
        "amq.rabbitmq.log",
        "amq.rabbitmq.trace",
        "amq.topic",
    ):
        delete_url = f"{url}/%2F/{exchange_name}"
        # 对每个交换器发送 DELETE 请求
        delete_response = requests.delete(delete_url, auth=auth)
        if delete_response.status_code == 204:
            print(f"删除 exchange {exchange_name} 成功！")
        else:
            print(
                f"删除 exchange 发生错误: {exchange_name}, 状态码: {delete_response.status_code}"
            )
